/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bsc.war.form;


import bsc.ejb.metadata.Framework;
import bsc.ejb.metadata.Lang;
import bsc.ejb.metadata.SMTP;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

//org.apache.commons.validator.GenericValidator

/**
 *
 * @author luisZavaleta
 */
public class NewAlert extends AbstractController  {

     @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
         HttpServletResponse response) throws Exception {
         HttpSession session = request.getSession();
        ModelAndView mav = new ModelAndView("addLang");


         try {
             List errors = new ArrayList<String>();
             DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
             //Alert alert = (Alert)Factory.getBean("Alert");
             SMTP smtp = (SMTP) Factory.getBean("SMTP");
             //Framework framework = (Framework)Factory.getBean("Framework");

             //alert.setName(request.getParameter("name"));
             //alert.setDescription(request.getParameter("description"));

             smtp.setDescription(request.getParameter("description"));
             smtp.setMail(request.getParameter("mail"));
             smtp.setName(request.getParameter("name"));
             smtp.setPassword(request.getParameter("password"));
             smtp.setSmtpServer(request.getParameter("smtpServer"));

             Framework framework = null;
             if (session.getAttribute("frameworkId") instanceof Integer) {
                 Integer frameworkId = (Integer) session.getAttribute("frameworkId");
                 framework = (Framework) Factory.getBean("Framework");
                 framework = delegate.findFramework(Long.valueOf(frameworkId));
                 smtp.setFramework(framework);
             }

             delegate.createSMTP(smtp);


             /*
             if(GenericValidator.isEmail(request.getParameter("email"))){
             alert.setMail(request.getParameter("mail"));
             }else{
             errors.add("error.email");
             }
             if(request.getParameter("password").equals(request.getParameter("password2"))){
             alert.setPassword(request.getParameter("password"));
             }else{
             errors.add("error.password");
             }
             alert.setSmtpServer(request.getParameter("smtp"));
              */
             //String idFramework = request.getParameter("framework");


             // if(StringUtils.isNumeric(idFramework)){
             //    framework = delegate.findFramework(Long.valueOf(idFramework));
             // }else{
             //   errors.add("error.framework");
             //}

             //alert.setFramework(framework);

             //delegate.createAlert(alert);

             Lang.Lenguage[] leng = Lang.Lenguage.values();
             mav.addObject("langs", leng);
             
         } catch (Exception e) {
         }


         return mav;
    }

}
